/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unibo.as.HeartsLogic;

import java.util.ArrayList;

/**
 *
 * @author lilith
 */
class Player {
    protected final String strPlayerID;
    protected ArrayList<Card> CardArray;
    protected Card playedCard = null;

    Player(String strPlayerID) 
    {
        this.strPlayerID = strPlayerID;
    }

    String getPlayerID() 
    {
        return strPlayerID;
    }

    void setCard(ArrayList<Card> PlayerCardArray) {
        sortCard(PlayerCardArray);
    }

    ArrayList<Card> getCards() {
        return CardArray;
    }
    
    private void sortCard(ArrayList<Card> PlayerCardArray)
    {
        ArrayList<Card> TempArray = new ArrayList<Card>();
        for(int i = 0; i < PlayerCardArray.size(); i++)
        {
            InsertSorted(TempArray, PlayerCardArray.get(i));
        }
        
        PlayerCardArray = TempArray;
    }
    
    private void InsertSorted(ArrayList<Card> PlayerCardArray, Card card)
    {
        int index = 0;
        
        boolean bContinue = true;
        
        for(int i = 0; (i < PlayerCardArray.size()) && bContinue; i++)
        {
            
            index = i;
            if(PlayerCardArray.get(i).nSeed > card.nSeed)
            {
                continue;
            }
            else
            {
                if(PlayerCardArray.get(i).nSeed == card.nSeed)
                {
                    if(PlayerCardArray.get(i).nCard > card.nCard)
                    {
                        continue;
                    }
                }
            }
            
            bContinue = false;
            
        }
        
        if(bContinue == true)
        {
            PlayerCardArray.add(card);
        }
        else
        {
            PlayerCardArray.add(index, card);
        }
    }
  

    boolean playCard(String tableSeed, boolean firstTurn, boolean playHearts, Card c) 
    {
        boolean bSeedExist = false;
        boolean bNoHeartsExist = false;
     
        Card card = null;
        
        for(int i = 0; i < CardArray.size(); i++)
        {   
            if(CardArray.get(i).isEqual(c))
            {
                card = CardArray.get(i);
            }
            
            if(CardArray.get(i).seedCard.equalsIgnoreCase(tableSeed))
            {
                bSeedExist = true;
            }

            if(!CardArray.get(i).seedCard.equalsIgnoreCase(Common.SEED_HEARTS))
            {
                bNoHeartsExist = true;
            }
        }
        
        if(card == null)
        {
            return false;
        }
        
        if(card.seedCard.equalsIgnoreCase(tableSeed))
        {
            CardArray.remove(card);
            return true;
        }
        else
        {   
            if(bSeedExist)
            {
                return false;
            }
            else
            {
                if (firstTurn && (card.numberCard.equalsIgnoreCase(Common.CARD_Q)) && (card.seedCard.equalsIgnoreCase(Common.SEED_SPADES)))
                {
                    return false;
                }
                
                if(((!playHearts && tableSeed.equalsIgnoreCase("")) || (firstTurn && bNoHeartsExist))  
                        && card.seedCard.equalsIgnoreCase(Common.SEED_HEARTS))
                {
                    return false;
                }
                
                CardArray.remove(card);
                playedCard = card;
                return true;
            }
        }
    }
    
    public void play()
    {
    }
    
}
